/*
 * @Description: 
 * @Author: mmzhang
 * @Date: 2021-12-10 15:47:09
 * @LastEditTime: 2023-02-20 10:48:39
 */
import fitClassApi from '@/config/server/api/fitClass'
import tool from '@/common/tool.js'
import dt from '@dt/dt'

let planList = []

function getPlan() {
  let data = {}

  if (tool.getStorage('isbuildFlag') == 'buildpage') {
    data.buildingId = dt.buildInfo().housesId
  }
  planList = []

  return fitClassApi.planChannel.channelList.fetch(data).then(res => {
    return res.map(item => {
      item.key = item.planId+'/'+item.channelIds.join(',')
      item.value = item.planName
      planList.push(item)
      return item
    })
  })
}

function create(opt = {}) { // key title
  return {
    title: opt.title,
    key: opt.key,
    placeholder: '请选择' + opt.title,
    disabled: opt.disabled || false,
    select: {
      filterable: true,
      multiple: true,
      options: opt.options || [],
    },
    load: (o) => {
      getPlan().then(res => {
        o.select.options = res
      })
    },
    change: opt.change,
    data: opt.data
  }
}


let planListNoBuild = []

function getNoBuildPlan() {
  planListNoBuild = []

  return fitClassApi.planChannel.planList.fetch({}).then(res => {
    return res.map(item => {
      item.key = item.id
      item.value = item.planName
      planListNoBuild.push(item)
      return item
    })
  })
}

function createNoBuild(opt = {}) { // key title
  return {
    title: opt.title,
    key: opt.key,
    placeholder: '请选择' + opt.title,
    disabled: opt.disabled || false,
    select: {
      filterable: true,
      multiple: true,
      options: opt.options || [],
    },
    load: (o) => {
      getNoBuildPlan().then(res => {
        o.select.options = res
      })
    },
    change: opt.change,
    data: opt.data
  }
}

export default {
  create,
  createNoBuild
}